package com.magicborrow.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.android.volley.VolleyError;
import com.magicborrow.Constants;
import com.magicborrow.R;
import com.magicborrow.activity.BorrowStuffDetailActivity;
import com.magicborrow.adapter.BorrowingAdapter;
import com.magicborrow.adapter.StuffCommentsRecordAdapter;
import com.magicborrow.beans.CommentListBean;
import com.magicborrow.utils.VolleyTool;
import com.magicborrow.views.RefreshRecyclerView;

import java.util.ArrayList;
import java.util.HashMap;

public class StuffCommentsRecordFragment extends Fragment implements VolleyTool.HTTPListener {
    private RefreshRecyclerView rc;
    private StuffCommentsRecordAdapter adapter;
    private ArrayList<CommentListBean.DataEntity.CommentBean> data;
    private int currentpage;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v=  inflater.inflate(R.layout.fragment_item_list, container, false);
        rc = (RefreshRecyclerView)v.findViewById(R.id.list);
        data = new ArrayList<>();

        adapter = new StuffCommentsRecordAdapter(data);
        rc.setAdapter(adapter);
        initData();
        v.findViewById(R.id.fab).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                rc.getManager().smoothScrollToPosition(rc.getRv(),null,0);
            }
        });

        return v;
    }

    private void initData() {
       /* int page; // 页码
        int size; // 每页返回条数

        int targetType; //类型  1:借用, 2：借出
        long targetId;  // 物品Id*/
        HashMap<String, String> params = new HashMap<>();
        params.put("page", "" + currentpage);
        params.put("size", "" + 20);
        params.put("targetType", "" + 1);
        params.put("targetId", "" + ((BorrowStuffDetailActivity) getActivity()).getStuff().getId());
        VolleyTool.get(Constants.URL_GET_COMMENT_LIST, params, this, 0, CommentListBean.class);
        rc.setOnPullListener(new RefreshRecyclerView.onPullListener() {
            @Override
            public void onPullListener() {
                currentpage++;
                initData();
            }
        });
        rc.setOnRefreshListener(new RefreshRecyclerView.OnRefreshListener() {
            @Override
            public void onRefreshListener() {
                currentpage = 0;
                initData();
            }
        });
    }


    @Override
    public <T> void onResponse(T t, int requestCode) {
        CommentListBean cb = (CommentListBean) t;
        if (cb.getCode() >= 0) {
            if (currentpage == 0)
                data.clear();
            rc.setIsLastPage(cb.getData().isLast());
            data.addAll(cb.getData().getContent());

            adapter.notifyDataSetChanged();

        } else {
            Toast.makeText(this.getContext(), cb.getMessage(), Toast.LENGTH_SHORT).show();
            if (currentpage > 0)
                currentpage--;
        }
        rc.setLoadFinish();
    }

    @Override
    public void onErrorResponse(VolleyError error, int requestCode) {
        rc.setLoadFinish();

        if (currentpage > 0)
            currentpage--;
    }
}
